{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import sys\n",
    "import numpy as np\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def num_zoom(min_num,max_num,x):\n",
    "    max_num = int(max_num)\n",
    "    min_num = int(min_num)\n",
    "    x = float(x)\n",
    "    return (x - min_num)/(max_num - min_num)*1.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(pd.read_csv('data/train_1w.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df = df.fillna('0')\n",
    "#df = df.sample(frac=1)\n",
    "#csv总行数\n",
    "num = len(df) - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def make_lst(file_path,start,end):\n",
    "    with open(file_path, 'w') as f:\n",
    "        index = 0\n",
    "        for row in range(start,end):\n",
    "            row_str     = str(index) + \"\\t\" + str(4) + \"\\t\" + str(5) + \"\\t\" + str(1069) +\"\\t\" + str(500) + \"\\t\"\n",
    "            coordinate  = str(df.iloc[row]['coordinate'])\n",
    "\n",
    "            if \";\" in coordinate:\n",
    "                coordinates = coordinate.split(\";\")\n",
    "                for val in coordinates:\n",
    "                    if len(val) == 0:\n",
    "                        continue\n",
    "                    areas = val.split(\"_\")\n",
    "                    row_str += str(0) + \"\\t\"\n",
    "                    i = 0\n",
    "                    \n",
    "                    xmin = 0\n",
    "                    ymin = 0\n",
    "                    xmax = 0\n",
    "                    ymax = 0\n",
    "                    for area in areas:\n",
    "                        if i == 0 :\n",
    "                            xmin = area\n",
    "                        elif i == 1:\n",
    "                            ymin = area\n",
    "                        elif i == 2:\n",
    "                            xmax = float(xmin) + float(area)\n",
    "                        elif i == 3:\n",
    "                            ymax = float(ymin) + float(area)\n",
    "                        i = i + 1\n",
    "                    #print (str(xmin) + \"=\" + str(ymin) + \"=\" + str(xmax) + \"=\" + str(ymax) + \"\")\n",
    "                    row_str += str(num_zoom(0,1069,xmin)) + \"\\t\"\n",
    "                    row_str += str(num_zoom(0,500,ymin)) + \"\\t\"\n",
    "                    row_str += str(num_zoom(0,1069,xmax)) + \"\\t\"\n",
    "                    row_str += str(num_zoom(0,500,ymax)) + \"\\t\"\n",
    "                   \n",
    "            elif coordinate != '0':\n",
    "                areas = coordinate.split(\"_\")\n",
    "                row_str += str(0) + \"\\t\"\n",
    "                i = 0\n",
    "                xmin = 0\n",
    "                ymin = 0\n",
    "                xmax = 0\n",
    "                ymax = 0\n",
    "                for area in areas:\n",
    "                    if  i == 0 :\n",
    "                        xmin = area\n",
    "                    elif i == 1:\n",
    "                        ymin = area\n",
    "                    elif i == 2:\n",
    "                        xmax = float(xmin) + float(area)\n",
    "                    elif i == 3:\n",
    "                        ymax = float(ymin) + float(area)\n",
    "                    i = i + 1\n",
    "                row_str += str(num_zoom(0,1069,xmin)) + \"\\t\"\n",
    "                row_str += str(num_zoom(0,500,ymin)) + \"\\t\"\n",
    "                row_str += str(num_zoom(0,1069,xmax)) + \"\\t\"\n",
    "                row_str += str(num_zoom(0,500,ymax)) + \"\\t\"\n",
    "                \n",
    "            else:\n",
    "                print(row)\n",
    "                continue\n",
    "            row_str += df.iloc[row]['name'] + \"\\n\"\n",
    "            f.write(row_str)\n",
    "            index = index + 1\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "make_lst('./image_val.lst',0,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2834\n",
      "3278\n",
      "3727\n",
      "4061\n",
      "5369\n",
      "8793\n",
      "8953\n",
      "9069\n"
     ]
    }
   ],
   "source": [
    "make_lst('./image_train.lst',100,1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
